<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 每月收支
                </el-breadcrumb-item>
                <el-button style="margin-left: 90%"
                           type="text"
                           icon="el-icon-refresh"
                           @click="getData"
                >刷新
                </el-button>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除
                </el-button>
                <el-date-picker
                    size="small"
                    @change="changeDate()"
                    v-model="query.sourceDate"
                    type="month"
                    placeholder="查询月份" style="margin-right: 5px;width: 12%">
                </el-date-picker>

                <el-button type="primary" icon="el-icon-plus" style="margin-left: 1%" @click="handleAdd">添加收支
                </el-button>
            </div>
            <el-table
                show-summary
                :summary-method="getSummaries"
                stripe
                :data="moneyMonthTable"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>

                <el-table-column prop="date" label="日期" width="95" align="center">
                    <template slot-scope="scope">{{ scope.row.sourceDate| dateFormat }}</template>
                </el-table-column>
                <el-table-column prop="moneyB" label="美团" width="70" align="center"></el-table-column>
                <el-table-column prop="moneyG" label="饿了么" width="70" align="center"></el-table-column>
                <el-table-column prop="moneyI" label="线上支付宝"  align="center"></el-table-column>
                <el-table-column prop="moneyH" label="微信支付" align="center"></el-table-column>
                <el-table-column prop="moneyK" label="智能机微信"  align="center"></el-table-column>
                <el-table-column prop="moneyJ" label="智能机支付宝"  align="center"></el-table-column>
                <el-table-column prop="moneyF" label="门店小程序"  align="center"></el-table-column>
                <el-table-column prop="moneyC" label="净现金" width="60" align="center"></el-table-column>
                <el-table-column prop="moneyM" label="口碑" width="60" align="center"></el-table-column>
                <el-table-column prop="moneyE" label="会员卡" width="60" align="center"></el-table-column>
                <el-table-column prop="moneyA" label="试吃" width="52" align="center"></el-table-column>
                <el-table-column prop="moneyD" label="报废" width="52" align="center"></el-table-column>
                <el-table-column prop="moneyL" label="赠送" width="52" align="center"></el-table-column>
                <el-table-column prop="amountMoney" label="合计(￥)" width="100" align="center"></el-table-column>
                <el-table-column prop="remark"  label="备注" align="center">

                </el-table-column>
                <el-table-column label="操作" width="140" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.row.id)"
                        >编辑
                        </el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.row.id)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!--添加销量弹窗-->
        <el-drawer
            :direction=direction
            :visible.sync="drawer"
            :with-header="false" size="25%">
            <div style="margin-top: 2%">
                <div style="margin-left: 48%">
                    <h3 style="color: #606266">{{ title }}</h3>
                </div>

                <el-form status-icon :model="moneyMonthData" ref="ruleForm" label-width="27%" class="demo-ruleForm"
                         style="margin-left: 8%;margin-top: 8%">
                    <el-form-item label="美团" prop="pass" style="margin-top: 3%">
                        <el-input type="text" @keyup.enter.native="savePayForm" @keyup.esc.native="savePayForm"
                                  placeholder="美团金额" v-model="moneyMonthData.moneyB" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>


                    <el-form-item label="饿了么" prop="pass" style="margin-top: 3%">
                        <el-input type="text" @keyup.enter.native="savePayForm" @keyup.esc.native="savePayForm"
                                  placeholder="饿了么金额" v-model="moneyMonthData.moneyG" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>

                    <el-form-item label="微信" prop="pass" style="margin-top: 3%">
                        <el-input type="text" @keyup.enter.native="savePayForm" @keyup.esc.native="savePayForm"
                                  placeholder="微信金额" v-model="moneyMonthData.moneyH" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="线上支付宝" prop="pass" style="margin-top: 3%">
                        <el-input type="text" @keyup.enter.native="savePayForm" @keyup.esc.native="savePayForm"
                                  placeholder="支付宝金额" v-model="moneyMonthData.moneyI" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="会员卡" prop="pass" style="margin-top: 3%">
                        <el-input type="text" @keyup.enter.native="savePayForm" @keyup.esc.native="savePayForm"
                                  placeholder="会员卡金额" v-model="moneyMonthData.moneyE" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="智能机微信" prop="pass" style="margin-top: 3%">
                        <el-input type="text" @keyup.enter.native="savePayForm" @keyup.esc.native="savePayForm"
                                  placeholder="智能机微信金额" v-model="moneyMonthData.moneyK" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="智能机支付宝" prop="pass" style="margin-top: 3%">
                        <el-input type="text" @keyup.enter.native="savePayForm" @keyup.esc.native="savePayForm"
                                  placeholder="智能机支付宝金额" v-model="moneyMonthData.moneyJ" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="门店小程序" prop="pass" style="margin-top: 3%">
                        <el-input type="text" @keyup.enter.native="savePayForm" @keyup.esc.native="savePayForm"
                                  placeholder="门店小程序金额" v-model="moneyMonthData.moneyF" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>

                    <el-form-item label="净现金" prop="pass" style="margin-top: 3%">
                        <el-input type="text" @keyup.enter.native="savePayForm" @keyup.esc.native="savePayForm"
                                  placeholder="净现金金额" v-model="moneyMonthData.moneyC" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>

                    <el-form-item label="口碑" prop="pass" style="margin-top: 3%">
                        <el-input type="text" @keyup.enter.native="savePayForm" @keyup.esc.native="savePayForm"
                                  placeholder="口碑金额" v-model="moneyMonthData.moneyM" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>

                    <el-form-item label="试吃" prop="pass" style="margin-top: 3%">
                        <el-input type="text" @keyup.enter.native="savePayForm" @keyup.esc.native="savePayForm"
                                  placeholder="试吃" v-model="moneyMonthData.moneyA" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>

                    <el-form-item label="报废" prop="pass" style="margin-top: 3%">
                        <el-input type="text" @keyup.enter.native="savePayForm" @keyup.esc.native="savePayForm"
                                  placeholder="智能机金额" v-model="moneyMonthData.moneyD" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="赠送" prop="pass" style="margin-top: 3%">
                        <el-input type="text" @keyup.enter.native="savePayForm" @keyup.esc.native="savePayForm"
                                  placeholder="赠送" v-model="moneyMonthData.moneyL" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>

                    <el-form-item label="备注" prop="pass" style="margin-top: 3%">
                        <el-input type="text" @keyup.enter.native="savePayForm" @keyup.esc.native="savePayForm"
                                  placeholder="备注" v-model="moneyMonthData.remark" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>

                    <el-form-item label="出账时间">
                        <el-date-picker
                            type="date"
                            placeholder="选择日期"
                            v-model="moneyMonthData.sourceDate"
                            value-format="yyyy-MM-dd"
                            style="width: 80%;">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item style="margin-left: 38%;margin-top: 3%">
                        <el-button @click="cancelPayFrom">取消</el-button>
                        <el-button type="primary" @click="savePayForm">保存</el-button>
                    </el-form-item>
                </el-form>
            </div>

        </el-drawer>
    </div>


</template>

<script>
import { getProductSalesByTime, getProductRecodeInfo, getLisProductRecode } from '@/api/productRecode';
import { delPay, getPayInfo, addUpdatePay, getPayList } from '@/api/PaySource';
import format from '@/utils/format';

export default {
    name: 'basetable',
    data() {
        return {
            query: {
                sourceDate: new Date(),
                pageIndex: 1,
                pageSize: 90
            },
            moneyMonthTable: [],
            moneyMonthData: {
                sourceDate: ''
            },
            multipleSelection: [],
            drawer: false,
            pageTotal: 0,
            ids: {
                ids: []
            },
            direction: 'rtl',
            title: ''
        };
    },
    created() {
        this.getData();
    },
    methods: {
        savePayForm() {
            addUpdatePay(this.moneyMonthData).then(res => {
                if (res.success) {
                    this.cancelPayFrom();
                    this.$message.success('添加成功');
                    this.getData();
                } else {
                    this.$message.warning(res.message);
                }

            });
        },

        cancelPayFrom() {
            this.drawer = false;
            this.moneyMonthData = {};


        },

        handleEdit(id) {
            getPayInfo(id).then(res => {
                if (res.success) {
                    this.title = '修改账单';
                    this.direction = 'rtl';
                    this.drawer = true;
                    this.moneyMonthData = res.data;
                } else {
                    this.$message.error(res.message);

                }

            });


        },

        handleAdd() {
            this.moneyMonthData = {};
            this.title = '录入账单';
            this.direction = 'ltr';
            this.drawer = true;
            this.moneyMonthData = {
                sourceDate: new Date()
            };

        },
        //表格合计计算
        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 1) {
                    sums[index] = '本月合计(￥)：';
                    return;
                }
                if (index === 0 || index===16) {
                    sums[index] = '';
                    return;
                }
                if (index === 17) {
                    sums[index] = format(this.query.sourceDate, 'YYYY 年 MM 月 ');
                    return;
                }
                const values = data.map(item => Number(item[column.property]));
                if (!values.every(value => isNaN(value))) {
                    sums[index] = values.reduce((prev, curr) => {
                        const value = Number(curr);
                        if (!isNaN(value)) {
                            return prev + curr;
                        } else {
                            return prev;
                        }
                    }, 0);
                    sums[index] = '￥' + sums[index] + '';


                } else {
                    sums[index] = ' ';
                }
            });

            return sums;
        },

        //日期选择框改变事件
        changeDate() {
            this.getData();

        },

        // 获取列表
        getData() {
            if (this.query.sourceDate == null) {
                this.$message.warning('请选择时间');
                return;
            }
            getPayList(this.query).then(res => {
                if (res.success) {
                    this.moneyMonthTable = res.data.records;
                    this.pageTotal = res.data.total || 0;
                } else {
                    this.$message.warning(res.message);
                    if (res.code === 10005) {
                        this.$router.push({ path: '/login' });
                    }
                }
            });
        },

        // 删除操作
        handleDelete(id) {
            if (id !== undefined) {
                this.ids.ids = [];
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                this.ids.ids.push(id);
                delPay(this.ids).then(res => {
                    if (res.success) {
                        this.$message.success('删除成功');
                        this.getData();
                    } else {
                        this.$message.error(res.message);
                    }

                });
            }).catch(() => {
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            for (let i = 0; i < this.multipleSelection.length; i++) {
                this.ids.ids.push(this.multipleSelection[i].id);

            }
            this.handleDelete();


        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style >
.el-drawer{

    overflow: scroll
}


.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 200px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}

.everyDay {
    display: inline-block;
    width: 20px;
    height: 20px;
    background-color: #409eff;
    color: #fff;
    border-radius: 50%;
}
</style>
